Random Audio File Player

level: intermediate

by Gordon Sweet, Gordon@gsweet.fsnet.co.uk

Home

LB Browser

4.03 Review

LED Display

Sprite Byte Masks

API Corner

Ini Files

Registration Keys

Eddie Fonts

Dragging a Popup

Random Audio Player

Eddie

Submissions

Index


Here is a little program I concocted recently; it just plays all the MID, MP3 and WMA files in any selected directory once only. Each selection is chosen at random. There are plenty of far more elaborate and clever software that do the same. These programs include Winamp and Windows Media Player. To be honest, I find these commercial softwares so awkward to setup, it is just simpler to create you own programs to do exactly what you want. This to me is the beauty of Liberty Basic.

I love to listen to music while tinkering on the keyboard, but now find it very difficult to relate to most of modern music as churned out by the majority of Radio. Even the UK BBC Radio 3, mainly dedicated to classical music, seems to delight in churning out a great deal of the tuneless modern efforts. So you may not be surprised to learn that even while typing out this article, I am running this program in the background, while listening to the likes of Frank Sinatra, Bing Crosby, George Formby, Frankie Vaughan, etc; all chosen at random by the PC. But let us now examine the code.

First of all I must express my appreciation to Alyce and others. Much of the API and other routines used to play the audio files are from Alyce's ebook. This is true as well of the API routines to enable the name of the current tune being played to show as the program title. This has the advantage that as the program is minimised to the lower toolbar, you can see the title displayed, which is also shown in the program’s window along with the previous ones played.

Since the program is intended to be used in the background while other tasks are involved, there is little point in using a window any larger than say 450 X 600; notice the second window opened to eliminate the buttons is given the same #handle as the first closed window. This simplifies closing the program at any time. The first audio file is selected by the user in the [music] section after setting the DefaultDirs$ to the root. DefaultDir$ is used because I cannot know where you keep your music files. This is then used to set the directory for all the other files, though you will notice chk$ filters out any unwanted files present. All the audio files are then played one at a time in random order. That file is then removed from the sound$ Array. Though all the tunes played are shown, MOD is used to clear the screen after 100 are shown.

You will see a short delay is included in the [loop] before a check is again made to see if the current file is finished playing to start another. This avoids excessive CPU usage. Notice some juggling is needed to determine if the path needs inserting in the title used in [play] and the window toolbar. No doubt you can improve on this code to suit you own needs.


    nomainwin : dim dir$(10,3) : dim sound$(501)
    WindowWidth = 450 : WindowHeight = 600
    button #p, "Start Music ", [music], UL 40, 400
    button #p, "**  QUIT ** ", [quit], UL 240, 400
    open "RANDOM MUSIC PLAYER" for graphics_nsb as #p
    #p "trapclose [quit]; font arial 18 bold; down"
    #p "color darkblue; place 70 100"
    #p "\MID, MP3, WMA PLAYER"
    #p "font arial 12 bold; place 40 200"
    #p "\Select the first music file to be played, all"
    #p "\the others will then be selected at random"
    #p "flush" : playwave "tada" : wait

[music]
    close #p : sp$=space$(12)
    open "FILES PLAYING" for text as #p
    #p "!trapclose [quit]" : #p "!font arial 10 bold"
    handle = hwnd(#p)
    calldll #user32, "GetParent", _
    handle AS long, _ 'get parent of this window
    parent AS long 'returns handle of parent window

    DefaultDir$ = left$(DefaultDir$,2)+"\"
    f$ ="*.mid;*.mp3;*.wma"
    filedialog "Select first music file",f$,File$
    if File$ = "" then [quit]

    File$ = upper$(File$)
    sFile$ = noPath$(File$)
    plen = len(File$)-len(sFile$)
    path$ = left$(File$,plen)
    files path$, "*.*", dir$()
    qtyFiles = val(dir$(0, 0))
    if qtyFiles < 4 then
        notice "You need at least 4 music files"
        goto [quit]
    end if
    if qtyFiles > 500 then
        qtyFiles = 500
        notice "Files limited to 500"
    end if
   'reformat the file information
    for x = 1 to qtyFiles
       dir$(x, 1) = right$("        " + dir$(x, 1), 9)
    next x : max = 1
    for x = 1 to qtyFiles
       temp$ =  upper$(dir$(x, 0))
       chk$ = right$(temp$,3) ' filter in audio files
       if chk$="MID" or chk$="MP3" or chk$="WMA" then
           sound$(max) = temp$
           max = max + 1
       end if
    next x : max = max - 1
    #p sp$;max;" music files in "
    #p path$
    tune = 0 : mus$=File$ : gosub [play]
    for tune = 1 to max
        pick = int(rnd(1) * max) + 1
        mus$=sound$(pick) : gosub [play]
        if tune mod 100 = 0 then #p "!cls"
        for n = pick to max ' remove file from list
            sound$(n) = sound$(n+1)
        next n
        max = max-1
    next tune
    notice "ALL FILES PLAYED" : goto [quit]

[play]
    title$ = mus$
    if tune > 0 then
        mus$=path$+mus$
    else
        title$ = noPath$(title$)
    end if
    call SetWindowText parent, title$ ' show title in window toolbar
    #p sp$;title$ : scan
    ' return ' to check all files
    calldll #kernel32,"Sleep",500 as ulong,r as void

    if musicOpen=1 then r$=mciSendString$("close music")
    mus$=GetShortPathName$(mus$)
    r$=mciSendString$("open "+mus$+" type MpegVideo alias music")
    r$=mciSendString$("status music length")
    musiclength=val(r$)
    musicOpen=1
    r$=mciSendString$("play music")
[loop]
    r$=mciSendString$("status music position")
    scan
    calldll #kernel32,"Sleep",100 as ulong,r as void
    if val(r$) >= musiclength then return
    goto [loop]

[quit]
    if musicOpen=1 then r$=mciSendString$("close music")
    close #p : end

Function GetShortPathName$(lPath$)
    lPath$=lPath$+Chr$(0)
    sPath$=Space$(256)
    lenPath=Len(sPath$)
    CallDLL #kernel32, "GetShortPathNameA",lPath$ As ptr,_
    sPath$ As ptr,lenPath As long,r As long
    GetShortPathName$=Left$(sPath$,r)
    End Function

function noPath$(t$)
    while instr(t$, "\")
    t$ = mid$(t$, 2)
    wend
    noPath$ = t$
    end function

Function mciSendString$(s$)
    buffer$=space$(1024)+chr$(0)
    calldll #winmm,"mciSendStringA",s$ as ptr,buffer$ as ptr,_
        1028 as long, 0 as long, r as long

        if r>0 then
            mciSendString$="error"
        else
            mciSendString$=buffer$
        end if
    End Function

sub SetWindowText handle, txt$
    calldll #user32, "SetWindowTextA",_
    handle as ulong,_
    txt$ as ptr,_
    result as boolean
end sub

Home

LB Browser

4.03 Review

LED Display

Sprite Byte Masks

API Corner

Ini Files

Registration Keys

Eddie Fonts

Dragging a Popup

Random Audio Player

Eddie

Submissions

Index